using System;
using System.Data;
using System.Data.SqlClient;

namespace MessageBoard
{
	/// <summary>
	/// Summary description for Post.
	/// </summary>
	public class Post : Connection
	{
		#region Fileds
		private int _Id;
		private Thread _Thread;
		private User _User;
		private string _Text;
		private Boolean _IsFirstPostInThread;
		private DateTime _PostDate;
		#endregion

		#region Constructors
		public Post()
		{

		}
		#endregion

		#region Functions
		public static Post Get(System.Data.DataRow dr)
		{
			Post post = new Post();
			post.Id = int.Parse(dr["Id"].ToString());
			post.Thread = Thread.Get(int.Parse(dr["ThreadId"].ToString()));
			post.User = User.Get(int.Parse(dr["UserId"].ToString()));
			post.Text = dr["Text"].ToString();
			post.IsFirstPostInThread = Convert.ToBoolean(dr["IsFirstPostInThread"].ToString());
			post.PostDate = Convert.ToDateTime(dr["PostDate"].ToString());
			return post;
		}

		public static Post Get(int id)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM [" + Connection.tblPost + "] WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static Post GetFirst(Thread thread)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT top 1 * FROM [" + Connection.tblPost + "] WHERE [ThreadId] = @ThreadId ORDER BY [Id]", DbConnection);
			getData.SelectCommand.Parameters.Add("@ThreadId", thread.Id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static Post GetLatest(Thread thread)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT top 1 * FROM [" + Connection.tblPost + "] WHERE [ThreadId] = @ThreadId ORDER BY [Id] DESC", DbConnection);
			getData.SelectCommand.Parameters.Add("@ThreadId", thread.Id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static System.Collections.ArrayList GetAll(Thread thread)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM [" + Connection.tblPost + "] WHERE [ThreadId] = @ThreadId", DbConnection);
			getData.SelectCommand.Parameters.Add("@ThreadId", thread.Id);
			getData.Fill(ds);
			foreach(System.Data.DataRow dr in ds.Tables[0].Rows)
			{
				arrayList.Add(Get(dr));
			}
			return arrayList;
		}

		public static System.Collections.ArrayList GetAll(User user)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM [" + Connection.tblPost + "] WHERE [ThreadId] = @UserId", DbConnection);
			getData.SelectCommand.Parameters.Add("@UserId", user.Id);
			getData.Fill(ds);
			foreach(System.Data.DataRow dr in ds.Tables[0].Rows)
			{
				arrayList.Add(Get(dr));
			}
			return arrayList;
		}

		public static int Count(Thread thread)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT count(*) FROM [" + Connection.tblPost + "] WHERE [ThreadId] = @ThreadId", DbConnection);
			getData.SelectCommand.Parameters.Add("@ThreadId", thread.Id);
			getData.Fill(ds);
			return int.Parse(ds.Tables[0].Rows[0][0].ToString());
		}

		public static int Count(Forum forum)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT count(*) FROM [" + Connection.tblPost + "] AS a INNER JOIN " + Connection.tblThread + " AS b ON [a].[ThreadId] = [b].[Id] WHERE [ForumId] = @ForumId", DbConnection);
			getData.SelectCommand.Parameters.Add("@ForumId", forum.Id);
			getData.Fill(ds);
			return int.Parse(ds.Tables[0].Rows[0][0].ToString());
		}
		#endregion

		#region Methods
		/// <summary>
		/// Add new post.
		/// </summary>
		public void Insert()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			INSERT INTO [" + Connection.tblPost + @"] 
			([ThreadId], [UserId], [Text], [IsFirstPostInThread], [PostDate])
			VALUES
			(@ThreadId, @UserId, @Text, @IsFirstPostInThread, GetDate());
			SELECT @@identity as i", DbConnection);
			getData.SelectCommand.Parameters.Add("@ThreadId", Thread.Id);
			getData.SelectCommand.Parameters.Add("@UserId", User.Id);
			getData.SelectCommand.Parameters.Add("@Text", Text);
			getData.SelectCommand.Parameters.Add("@IsFirstPostInThread", IsFirstPostInThread);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				Id = int.Parse(ds.Tables[0].Rows[0]["i"].ToString());
			}
			User.AddNewPost();
		}

		/// <summary>
		/// update the post.
		/// </summary>
		public void Update()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			UPDATE [" + Connection.tblPost + @"] SET 
			[ThreadId] = @ThreadId,
			[Text] = @Text
			WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@ThreadId", Thread.Id);
			getData.SelectCommand.Parameters.Add("@Text", Text);
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
		}

		/// <summary>
		/// delete the post and all the related data.
		/// </summary>
		public void Delete()
		{
			// Delete the related posts if this post is the first in the thread.
			DeletePosts();			

			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			DELETE FROM [" + Connection.tblPost + @"] WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
			if(this.IsFirstPostInThread)
				this.Thread.Delete();

			User.PostDeleted();
		}

		/// <summary>
		/// Delete the related threads.
		/// </summary>
		private void DeletePosts()
		{
			if(this.IsFirstPostInThread)
			{
				foreach(Post post in Post.GetAll(this.Thread))
				{
					if(!post.IsFirstPostInThread)
						post.Delete();
				}				
			}
		}

		#endregion

		#region Properties
		/// <summary>
		/// The Id of the post.
		/// </summary>
		public int Id
		{
			get
			{
				return _Id;
			}
			set
			{
				_Id = value;
			}
		}

		/// <summary>
		/// The thread.
		/// </summary>
		public Thread Thread
		{
			get
			{
				return _Thread;
			}
			set
			{
				_Thread = value;
			}
		}

		/// <summary>
		/// The user who add the post.
		/// </summary>
		public User User
		{
			get
			{
				return _User;
			}
			set
			{
				_User = value;
			}
		}

		/// <summary>
		/// The text.
		/// </summary>
		public string Text
		{
			get
			{
				return _Text;
			}
			set
			{
				_Text = value;
			}
		}

		/// <summary>
		/// Is this post the first post in the thread.
		/// </summary>
		public Boolean IsFirstPostInThread
		{
			get
			{
				return _IsFirstPostInThread;
			}
			set
			{
				_IsFirstPostInThread = value;
			}
		}

		/// <summary>
		/// The date of post.
		/// </summary>
		public DateTime PostDate
		{
			get
			{
				return _PostDate;
			}
			set
			{
				_PostDate = value;
			}
		}		
		#endregion
	}
}
